<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>编辑资料</title>
		<meta name="Description" content="" />
		<meta name="Keywords" content="" />
		<meta name="format-detection" content="telephone=no, email=no" />
		<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
		<!-- 忽略页面中的数字识别为电话，忽略email识别 -->
		<meta name="applicable-device" content="mobile" />
		<meta name="apple-mobile-web-app-title" content="" />
		<!-- 添加到主屏后的标题（iOS 6 新增） -->
		<meta name="apple-mobile-web-app-capable" content="yes" />
		<!-- 是否启用 WebApp 全屏模式 -->
		<meta name="HandheldFriendly" content="true" />
		<!-- 针对手持设备优化，主要是针对一些老的不识别viewport的浏览器，比如黑莓 -->
		<meta name="author" content="Jack" />
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<script src="../../js/flexible.js" type="text/javascript" charset="utf-8"></script>
		<link rel="stylesheet" type="text/css" href="../../style/jquery_css/weui.css" />
		<link rel="stylesheet" type="text/css" href="../../style/jquery_css/jquery-weui.css" />
		<link rel="stylesheet" type="text/css" href="../../style/icon_font/iconfont.css" />
		<link rel="stylesheet" type="text/css" href="../../style/car.css" />
		<link rel="stylesheet" type="text/css" href="../../style/pub_foot.css" />
		<link rel="stylesheet" type="text/css" href="../../style/main.css" />
		<link rel="stylesheet" type="text/css" href="../../style/icon.css" />
	</head>

	<body>
		<div class="header-nav">
			<div class="cen-mony color-guess">
				<div class="header-flex">
					<i class="return-2" onclick="window.history.go(-1)"></i>
				</div>
				<div class="header-flex">编辑资料</div>
				<div class="header-flex"></div>
			</div>
		</div>
		<div class="compile-max-box">
			<div class="compile-cen">
				<div class="comp-flex-box" onclick="main_obj.uploading()">
					<div class="copm-flex1"><img src="../../images/header.png" class="header-img" style="object-fit: cover;"/></div>
					<div class="copm-flex2"><span class="uploding">上传头像</span></div>
					<div class="copm-flex3"><i class="iconfont icon-you icon-right"></i></div>
				</div>
				<input id="upload" name="image" accept="image/*" type="file" style="display: none">
				<div class="comp-flex-box" onclick="main_obj.change_name()">
					<div class="copm-flex1"><span>姓名</span></div>
					<div class="copm-flex2"><span class="uploding" id="user-name"></span></div>
					<div class="copm-flex3"><i class="iconfont icon-you icon-right"></i></div>
				</div>
				<div class="comp-flex-box">
					<div class="copm-flex1"><span>手机号</span></div>
					<div class="copm-flex2"><span class="uploding" id="user-phone"></span></div>
					<div class="copm-flex3"></div>
				</div>
				<div class="comp-flex-box">
					<div class="copm-flex1"><span>邀请码</span></div>
					<div class="copm-flex2"><span class="uploding" id="invitation"></span></div>
					<div class="copm-flex3"><span onclick="copy_fn('invitation')">复制</span></div>
				</div>
			</div>
		</div>
	</body>
	<script src="../../js/jquery.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../js/weiUI/jquery-weui.min.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../js/api/api_url.js" type="text/javascript" charset="utf-8"></script>
	<script src="../../js/api/port_url.js" type="text/javascript" charset="utf-8"></script>
	<script type="text/javascript">
		var user_arr = JSON.parse(sessionStorage.getItem('users_info'));
		$(function() {
			main_obj.user_info();
		})
		var main_obj = {
			uploading: function() {
				$("#upload").click();
				$("#upload").on("change", function() {
					var a = $(this)[0].files[0];
					var formData = new FormData();
					formData.append("image", a);
					formData.append("access_token", '41ab23e7efb238797fe39cc30f6b21d3');
					formData.append("user_token", user_arr.user_token);
					formData.append("user_id", user_arr.id);
					$.showLoading(' ');
					$.ajax({
						url: config_URL.api + URL.upload, //请求的url地址
						dataType: "json", //返回格式为json
						data: formData,
						processData: false, // 使数据不做处理
						contentType: false,
						type: "POST", //请求方式
						success: function(res) {
							console.log(res)
							if(res.code == 0) {
								var receive_img = res.data.headimg;
								$('.header-img').attr('src', '' + receive_img);
								$.hideLoading();
								$.toast("上传成功!", 'text');
								main_obj.user_info();
							} else if(res.code == 1000005) {
								$.toast("你的账号已在其地方登录，请重新登录！", 'text');
								setTimeout(function() {
									window.parent.location.href = '../login/login.html'
								}, 3000)

							} else if(res.code == 1020001) {
								$.toast(res.msg, 'text');
							} else {
								$.toast(res.msg, 'text');
							}
						},
						error: function(rul) {
							console.log('网络出错')
						}
					});

					     
				});
			},
			//用户信息
			user_info: function() {
				$.ajax({
					url: config_URL.api + URL.userInfo, //请求的url地址
					dataType: "json", //返回格式为json
					data: {
						access_token: '41ab23e7efb238797fe39cc30f6b21d3',
						user_token: user_arr.user_token,
						user_id: user_arr.id
					},
					type: "POST", //请求方式
					success: function(res) {
						console.log(res)
						var data = res.data;
						if(res.code == 0) {
							$('.welcome').html(data.name);
							$('#is_show_btn').hide();
							if(data.headimg == '') {
								$('.header-img').attr('src', '../../images/header.png');
							} else {
								$('.header-img').attr('src', '' + data.headimg);
							}
							$('#user-name').html(data.name);
							$('#user-phone').html(data.phone);
							$('#invitation').html(data.invitation);
						} else {
							$.toast(res.msg, "text");
						}

					},
					error: function(rul) {
						console.log('网络出错')
					}
				});
			},
			change_name_fn: function(text) {
				
				$.ajax({
					url: config_URL.api + URL.userEdit, //请求的url地址
					dataType: "json", //返回格式为json
					data: {
						access_token: '41ab23e7efb238797fe39cc30f6b21d3',
						user_token: user_arr.user_token,
						user_id: user_arr.id,
						name: text
					},
					type: "POST", //请求方式
					success: function(res) {
						console.log(res)
						var data = res.data;
						if(res.code == 0) {
							$.toast('修改成功', "text");
							main_obj.user_info();
						} else {
							$.toast(res.msg, "text");
						}

					},
					error: function(rul) {
						console.log('网络出错')
					}
				});
			},
			//修改名字
			change_name: function() {
				$.prompt({
					title: "输入姓名",
					onOK: function(text) {
						console.log(text);
						if(text == '') {
							$.toast("请输入用户名!", "text");
							return false;
						}
						main_obj.change_name_fn(text);
					},
					onCancel: function() {
//						console.log("取消了");
					},
					input: ''
				});
			}
		}

		function copy_fn(code_name) {
			var txt = $('#' + code_name + '').text();
			Copy(txt);
		}
		// 复制微信号函数
		function Copy(str) {
			var save = function(e) {
				e.clipboardData.setData('text/plain', str);
				e.preventDefault();
			};
			document.addEventListener('copy', save);
			document.execCommand('copy');
			document.removeEventListener('copy', save);
			$.toast('复制成功', "text");
		}
	</script>

</html>